package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Keyword;
import com.kt.uokos.yimo.example.KeywordExample;
import com.kt.uokos.yimo.mapper.KeywordMapper;
import com.kt.uokos.yimo.service.KeyWordService;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class KeywordServiceImpl implements KeyWordService {



    @Resource
    private KeywordMapper keywordMapper;

    @Override
    public List<Keyword> querySelective(String keyword,String url,Integer page,Integer limit,String sort,String order) {
        KeywordExample example = new KeywordExample();
        KeywordExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if(!StringUtils.isEmpty(url)){
            criteria.andUrlLike("%"+url+"%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page,limit);
        return keywordMapper.selectByExample(example);
    }

    @Override
    public void create( Keyword keyword) {
        keyword.setAddTime(LocalDateTime.now());
        keyword.setUpdateTime(LocalDateTime.now());
        keywordMapper.insertSelective(keyword);
    }

    @Override
    public void delete(Integer id) {
        keywordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update( Keyword keyword) {
        keyword.setUpdateTime(LocalDateTime.now());
        return keywordMapper.updateByPrimaryKeySelective(keyword);
    }

    @Override
    public Keyword queryDefault(Integer defaultId) {
        return keywordMapper.selectByPrimaryKey(defaultId);
    }

    @Override
    public List<Keyword> queryHot() {
        KeywordExample example = new KeywordExample();
        KeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIsHotEqualTo(true).andDeletedEqualTo(false);
        return keywordMapper.selectByExample(example);
    }

    @Override
    public List<Keyword> queryByKeyword(String keyword, Integer page, Integer limit) {
        KeywordExample example = new KeywordExample();
        example.setDistinct(true);
        example.or().andKeywordLike("%"+keyword+"%").andDeletedEqualTo(false);
        return keywordMapper.selectByExample(example);
    }
}
